package com.zjhd.sealcutting.service.impl;

import com.google.common.collect.Maps;
import com.zjhd.sealcutting.dao.ExhibitionInfoMapper;
import com.zjhd.sealcutting.dao.UserInfoMapper;
import com.zjhd.sealcutting.domain.ExhibitionInfo;
import com.zjhd.sealcutting.domain.UserInfo;
import com.zjhd.sealcutting.param.ExhibitionSearchQuery;
import com.zjhd.sealcutting.service.ExhibitionInfoService;
import com.zjhd.sealcutting.utils.DateUtil;
import com.zjhd.sealcutting.utils.response.ResponseResult;
import com.zjhd.sealcutting.utils.response.ResultGenerator;
import com.zjhd.sealcutting.vo.SearchResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * TODO 展览管理
 *
 * @author wangchao
 * @version 1.0
 * @date 2020/11/27 11:22
 */
@Service
public class ExhibitionInfoServiceImpl implements ExhibitionInfoService {

    @Autowired
    private ExhibitionInfoMapper exhibitionInfoMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * TODO  分页查询展览
     * @author wangchao
     * @date 2020/11/27 11:27
     * @param
     * @return
     */
    @Override
    public SearchResult queryExhibitionParamlist(ExhibitionSearchQuery param) throws Exception {
        //定义map 封装查询参数到map中
        Map<String, Object> map = Maps.newLinkedHashMap();
        if(param.getType()!=null){
            map.put("type",param.getType());
        }
        if(param.getExhibitionStatus()!=null){
            map.put("exhibitionStatus",param.getExhibitionStatus());
        }
        //当前页  分页数
        map.put("pageIndex", (Integer.parseInt(param.getPageIndex()) - 1) * Integer.parseInt(param.getPageSize()));
        map.put("pageSize", Integer.parseInt(param.getPageSize()));
        //查询数据
        List<ExhibitionInfo> exhibitionInfos=exhibitionInfoMapper.queryParamExhibitionList(map);
        long totalCount=exhibitionInfoMapper.queryParamExhibitionListCount(map);
        if(exhibitionInfos.size()>0){
            return new SearchResult.Builder<List<ExhibitionInfo>>()
                    .setCurrent(Integer.parseInt(param.getPageIndex()))
                    .setPageSize(Integer.parseInt(param.getPageSize()))
                    .setTotalCount(totalCount)
                    .setPagedItems(exhibitionInfos == null ? new ArrayList() : exhibitionInfos)
                    .build();
        }else {
            return new SearchResult.Builder().setPagedItems(new ArrayList()).build();
        }
    }

    /**
     * TODO  新增展览
     * @author wangchao
     * @date 2020/11/27 11:27
     * @param
     * @return
     */
    @Override
    @Transactional
    public ResponseResult insertExhibition(ExhibitionInfo exhibitionInfo) throws Exception {
        try {
            int i=exhibitionInfoMapper.insert(exhibitionInfo);
            if(i>0){
                return ResultGenerator.genSuccessResult("新增展览成功!");
            }else {
                return ResultGenerator.genFailResult("新增展览失败!");
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return ResultGenerator.genFailResult("新增展览失败!");
    }

    /**
     * TODO 修改展览
     * @author wangchao
     * @date 2020/11/27 11:28
     * @param
     * @return
     */
    @Override
    @Transactional
    public ResponseResult updateExhibition(ExhibitionInfo exhibitionInfo) throws Exception {
        try {
            int i=exhibitionInfoMapper.updateByPrimaryKeySelective(exhibitionInfo);
            if(i>0){
                return ResultGenerator.genSuccessResult("修改展览成功!");
            }else {
                return ResultGenerator.genFailResult("修改展览失败!");
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return ResultGenerator.genFailResult("修改展览失败!");
    }

    /**
     * TODO  查询展览详情
     * @author wangchao
     * @date 2020/11/27 11:28
     * @param
     * @return
     */
    @Override
    public ExhibitionInfo queryByIdExhibition(Integer id) throws Exception {
        return exhibitionInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * TODO 删除展览
     * @author wangchao
     * @date 2020/11/27 11:28
     * @param
     * @return
     */
    @Override
    @Transactional
    public boolean delExhibition(Integer id) throws Exception {
        boolean bool=false;
        int i=exhibitionInfoMapper.deleteByPrimaryKey(id);
        if(i>0){
            bool=true;
        }
        return bool;
    }

    /**
     * TODO 查询所有征集中的展览
     * @author wangchao
     * @date 2020/12/14 10:50
     * @param
     * @return
     */
    @Override
    public List<ExhibitionInfo> queryCentralizationExhibition(Integer userId) throws Exception {
        List<ExhibitionInfo> exhibitionInfos=new ArrayList<>();
        //查询用户信息
        UserInfo userInfo=userInfoMapper.selectByPrimaryKey(userId);
        if(userInfo!=null){
            if(userInfo.getUserGroup().equals("1")){
                exhibitionInfos=exhibitionInfoMapper.queryCentralizationExhibition();
            }else {
                exhibitionInfos=exhibitionInfoMapper.queryByUserIdCentralizationExhibition(userId);
            }
        }
        return exhibitionInfos;
    }
}
